<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>微博首页</h1>
<div>
    <div v-if="user==''"><!--未登录-->
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <div v-if="user!=''"><!--已登录-->
        <h1>欢迎{{user.nick}}</h1>
        <a href="/send.html">发布微博</a>
        <a href="/myweibo.html">我的微博</a>
        <a href="javascript:void (0)" @click="logout()">登出</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <h3>
            <a :href="'/detail.html?id='+weibo.id">
                {{weibo.author}}:{{weibo.title}}
            </a>
        </h3>
        <a :href="'/detail.html?id='+weibo.id">
            <img :src="weibo.url" style="width: 200px" alt="">
        </a>
    </div>
</div>
<!--引入axios和vue-->
<script src="/js/axios.min.js"></script><!--.是当前文件的根目录可以省略,即/xxx/xxx-->
<script src="/js/vue.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            user:"",
            arr:[]
        },
        created:function () {
            //获取当前登录的用户对象
            axios.get("/currentUser").then(function (response) {
                v.user = response.data;
            })
            //获取所有微博数据
            axios.get("/weibo/select").then(function (response) {
                v.arr = response.data;
            })
        },
        methods:{
            logout:function () {
                if(confirm("您确定退出登录吗?")){
                    axios.get("/logout").then(function () {
                        location.reload();//刷新页面
                    })
                }
            }
        }
    })
</script>
</body>
</html>